// mircoApp/share_chain/share_chain_dealDetail/share_chain_dealDetail.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    active: 2,
    inquire_act: 2,
    navChild: "0",
    keyword: "",
    showDate: false,
    startTimeShow: false,
    endTimeShow: false,

    minDate: new Date().getTime(),
    maxDate: new Date().getTime(),
    currentDate_start: new Date().getTime(),
    currentDate_end: "",
    plugnNmae: "",
    topTabs: [],
    summary: [],
    listData: [],
    total: 0,

    page: 1,
    isLoadMore: true,
    total_page: 0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    if (options.plugn) {
      this.setData({
        plugnNmae: options.plugn
      })
    }
    let topTabs = [];
    if (options.plugn == 'deal') {
      wx.setNavigationBarTitle({
        title: '交易明细',
      })
      topTabs = [{
          name: "收银台交易明细",
          ind: 2
        },
        {
          name: "门店订单交易明细",
          ind: 1
        }
      ];
    } else if (options.plugn == "inquire") {
      wx.setNavigationBarTitle({
        title: '占位查询',
      })
      topTabs = [{
          name: "收银台占位查询",
          ind: 2
        },
        {
          name: "门店占位查询",
          ind: 1
        }
      ];
    } else if (options.plugn == "store") {
      wx.setNavigationBarTitle({
        title: '门店交易',
      })
      topTabs = [{
          name: "收银台交易统计",
          ind: 2
        },
        {
          name: "门店订单交易统计",
          ind: 1
        }
      ];
    }
    console.log(topTabs)
    this.setData({
      topTabs: topTabs
    })
    console.log(this.data.topTabs)
    this.getData();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    var nowYear = new Date().getFullYear() - 25;
    var nowDate = new Date(`${nowYear}/01/01`).getTime();
    this.setData({
      minDate: nowDate
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    if (this.data.isLoadMore) {
      this.getMoreData();
    } else {
      console.log('没有更多了');
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
  updateList(e) {
    console.log('updateList', e.detail)
    let {
      item,
      index
    } = e.detail;
    this.data.listData[index].show = (item.show ? false : true);

    this.setData({
      ['listData[' + index + '].show']: this.data.listData[index].show
    })
  },
  tapComfirm() {
    if (app._isTextEmpty(this.data.currentDate_start)) {
      app.tips('请选择开始时间');
      return
    }
    if (app._isTextEmpty(this.data.currentDate_end)) {
      app.tips('请选择结束时间');
      return
    }
    let start = parseInt(this.data.currentDate_start / 1000);
    let end = parseInt(this.data.currentDate_end / 1000);
    console.log(start, end);
    if (start >= end) {
      app.tips("开始时间不能小于或等于结束时间");
      return;
    }
    this.setData({
      showDate: false
    })
    this.initData();
    this.getData();
  },
  tapTimeBtn(e) {
    let {
      key
    } = e.currentTarget.dataset;
    if (key == "startTimeShow") {
      this.setData({
        startTimeShow: true,
        endTimeShow: false
      })
      console.log(this.data.currentDate_end)
    } else {
      this.setData({
        startTimeShow: false,
        endTimeShow: true,
        currentDate_end: this.data.currentDate_start
      })
    }
  },
  onInput(e) {
    this.setData({
      currentDate: e.detail,
      currentDate_start: e.detail
    })
  },
  onInput1(e) {
    this.setData({
      currentDate: e.detail,
      currentDate_end: e.detail
    })
  },
  headTabChange(e) {
    let {
      name
    } = e.detail;
    this.setData({
      active: name,
      inquire_act: 2,
      navChild: "0"
    })
    this.initData();
    this.getData();
  },
  searchPost() {
    this.initData();
    this.getData();
  },
  initData() {
    this.setData({
      page: 1,
      isLoadMore: true,
      total_page: 0,
      listData: [],
      summary: []
    })
  },
  inquireChange(e) {
    let {
      name
    } = e.detail;
    this.setData({
      inquire_act: name
    })
    this.initData();
    this.getData();
  },
  tapNav(e) {
    let {
      ind
    } = e.currentTarget.dataset;
    this.setData({
      navChild: ind
    })
    if (ind == '4') {
      this.setData({
        showDate: true,
        startTimeShow: true,
        endTimeShow: false,
      })
      return
    }
    this.initData();
    this.getData();
  },
  closePopup() {
    if (this.data.navChild == '4') {
      if (!this.data.currentDate_start || !this.data.currentDate_end) {
        this.setData({
          navChild: '0',
          showDate: false
        })
        return
      }
      this.initData();
      this.getData();
      this.setData({
        showDate: false
      })
      return
    }

    this.setData({
      showDate: false
    })
  },
  getData() {
    let urlStr = "";
    let json = {
      order_type: this.data.active,
      kwd: this.data.keyword ? this.data.keyword : "",
      time_type: Number(this.data.navChild)
    };
    if (this.data.navChild == "4") {
      if (this.data.plugnNmae == "inquire" && this.data.inquire_act == 1) {
        return;
      }
      if (!this.data.currentDate_start || !this.data.currentDate_end) {
        this.tapNav("4");
        return;
      }
      json.start_time = parseInt(this.data.currentDate_start / 1000);
      json.end_time = parseInt(this.data.currentDate_end / 1000);
    }
    if (this.data.plugnNmae == "deal" || this.data.plugnNmae == "store") {
      urlStr = "plugin.share-chain.frontend.trade-log.index";
    } else {
      urlStr = "plugin.share-chain.frontend.trade-log.queueIndex";
      json.status = Number(this.data.inquire_act);
    }
    app._getNetWork({
      url: app.getNetAddresss(urlStr),
      data: json,
      success: (resdata) => {
        let res = resdata.data
        if (res.result == 1) {
          this.data.isLoadMore = true;
          this.data.total_page = res.data.last_page;
          if (!this.data.total_page) {
            this.data.total_page = 0;
          }
          this.setData({
            summary: res.data.summary,
            listData: res.data.data,
            total: res.data.total,
          })
        }
      },
      fail: function (res) {}
    });
  },
  getMoreData() {
    let urlStr = "";
    this.data.isLoadMore = false; // 防止多次请求分页数据
    if (this.data.page >= this.data.total_page) {
      return;
    } else {
      this.setData({
        page: this.data.page + 1
      })
      let json = {
        order_type: this.data.active,
        kwd: this.data.keyword ? this.data.keyword : "",
        time_type: Number(this.data.navChild),
        page: this.data.page
      };
      if (this.data.navChild == "4") {
        if (this.data.plugnNmae == "inquire" && this.data.inquire_act == 1) {
          return;
        }
        if (!this.data.currentDate_start || !this.data.currentDate_end) {
          this.tapNav("4");
          return;
        }
        json.start_time = parseInt(this.data.currentDate_start / 1000);
        json.end_time = parseInt(this.data.currentDate_end / 1000);
      }
      if (this.data.plugnNmae == "deal" || this.data.plugnNmae == "store") {
        urlStr = "plugin.share-chain.frontend.trade-log.index";
      } else {
        urlStr = "plugin.share-chain.frontend.trade-log.queueIndex";
        json.status = Number(this.data.inquire_act);
      }
      urlStr = app.getNetAddresss(urlStr);
      app._postNetWork({
        url: urlStr,
        data: json,
        success: (resdata) => {
          let res = resdata.data
          if (res.result == 1) {
            this.data.isLoadMore = true;
            this.setData({
              listData: this.data.listData.concat(res.data.data)
            })
          } else {
            this.data.page = this.data.page - 1;
            this.isLoadMore = false
            wx.showToast({
              icon: 'none',
              title: res.msg,
              duration: 1500
            });
          }
        },
        fail: function (res) {
          console.log(res);
        }
      });
    }
  }
})